package com.hqw.jzdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;

import com.fengfei.ffadsdk.AdViews.Splash.FFSpalshListener;
import com.fengfei.ffadsdk.AdViews.Splash.FFSplashManager;
import com.fengfei.ffadsdk.Common.Util.FFAdLogger;

public class SplashActivity extends AppCompatActivity {

    private FFSplashManager ffsplash;
    private boolean isRemove = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ffsplash = new FFSplashManager(this);
        ViewGroup container = findViewById(R.id.container);

        container.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {

            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                isRemove = true;
                FFAdLogger.d("容器从 window 移除");
            }
        });

        ffsplash.requestAd(this, BuildConfig.appId, BuildConfig.splashId, container, new FFSpalshListener() {
            @Override
            public void onAdDisplay() {
                // 展示
                Log.i("FFAd-splash", "onAdDisplay()");
            }

            @Override
            public void onAdClose() {
                Log.i("FFAd-splash", "onAdClose()");
                if (!isRemove) {
                    goBack();
                }
            }

            @Override
            public void onAdRecieved() {
                // 没用
                Log.i("FFAd-splash", "onAdRecieved()");
            }

            @Override
            public void onAdClick() {
                // 没用
                Log.i("FFAd-splash", "onAdClick");
                if (ffsplash.getClickType().equals("1")) {
                    // 端内跳转
                } else {
                    // 其他不做处理
                }
            }

            @Override
            public void onAdFailed(String s) {
                // 失败
                Log.e("FFAd-splash", "onAdFailed(): " + s);
                if (!isRemove) {
                    goBack();
                }
            }

            @Override
            public void onAdSpreadNotifyCallback(String s, ViewGroup viewGroup, int i, int i1) {
                // 没用
            }
        }, true);

    }

    private void goBack() {
        this.finish();
    }

    @Override
    protected void onPause() {
        FFAdLogger.i("FFSplashManager.onPause()");
        if (ffsplash != null) ffsplash.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        FFAdLogger.i("FFSplashManager.onResume()");
        if (ffsplash != null) ffsplash.onResume();
        super.onResume();
    }

    @Override
    protected void onStop() {
        FFAdLogger.i("FFSplashManager.onStop()");
        if (ffsplash != null) ffsplash.onStop();
        super.onStop();
    }

}
